package httpEntry

import (
	"end/config"
	"github.com/gin-gonic/gin"
	"net/http"
	"strings"
)

const Authorization = "Authorization"

type BaseResponse struct {
	Code    int    `json:"code"`
	Error   string `json:"error"`
	Message string `json:"message"`
}

const ERROR10001 = "没有登录"
const ERROR10002 = "登录失效"
const ERROR10003 = "不是商家"
const ERROR10004 = "没有通过审核"
const ERROR10005 = "查询错误"
const ERROR10006 = "参数不够"
const ERROR10007 = "管理员输入店员错误"
const ERROR10008 = "店员插入错误"
const ERROR10009 = "删除店员错误"
const ERROR10010 = "文件上传失败"
const ERROR10011 = "错误，权限不够"
const ERROR10012 = "不是管理员"
const ERROR10013 = "管理员权限不足"
const ERROR10014 = "服务器内部错误"
const ERROR10015 = "积分不足"
const ERROR10016 = "库存不足"
const ERROR10017 = "商家申请错误"
const ERROR10018 = "不是店员"
const ERROR10019 = "二维码超时"
const ERROR10020 = "二维码错误"
const ERROR10022 = "核验失败，是否已经核验过或者商家以下架此商品"
const ERROR10023 = "活动已经结束了！！"
const ERROR10024 = "已经报名此活动"
const ERROR10025 = "活动已经无法报名！！"
const ERROR10026 = "已经注册管理员"
const ERROR10027 = "商品已经下架"
const ERROR10028 = "人数已满"
const ERROR10029 = "参数错误"
const ERROR10030 = "活动未报名"
const ERROR10031 = "活动进行时间错误"

const STATUS1 = config.STATUS1
const STATUS2 = config.STATUS2
const STATUS3 = config.STATUS3
const STATUS6 = config.STATUS6

// LoginMiddleware 登录模块判断

func LoginMiddleware() gin.HandlerFunc {
	return func(context *gin.Context) {
		token := context.GetHeader(Authorization)
		if token == "" {
			context.JSON(http.StatusOK, BaseResponse{
				Code:  10001,
				Error: ERROR10001,
			})
			context.Abort()
			return
		}
		allinfo := config.AnalysisToken(token)
		if allinfo.UserId == 0 {
			context.JSON(http.StatusOK, BaseResponse{
				Code:  10002,
				Error: ERROR10002,
			})
			context.Abort()
			return
		}
		context.Set("allinfo", allinfo)
		// 设置基础的用户状态
		userInfo := config.UserInfo{
			Id:       allinfo.UserId,
			Identity: "user",
		}
		context.Set("userinfo", userInfo)
		context.Next()
	}
}

func BusinessMiddleware() gin.HandlerFunc {
	return func(context *gin.Context) {
		allinfo := context.MustGet("allinfo").(config.AllInfo)
		// 判断是否有商家权限
		if allinfo.Level&config.BUSINESS == 0 {
			context.JSON(http.StatusOK, BaseResponse{
				Code:  10003,
				Error: ERROR10003,
			})
			context.Abort()
			return
		}
		var userInfo = config.UserInfo{}
		for i := range allinfo.Additionals {
			if allinfo.Additionals[i].Type == "business" {
				userInfo.Id = allinfo.Additionals[i].Id
				userInfo.Identity = "business"
				break
			}
		}
		context.Set("userinfo", userInfo)
		context.Next()
	}
}

// Admin1Middleware 是否是一级管理员
func Admin1Middleware() gin.HandlerFunc {
	return func(context *gin.Context) {
		allinfo := context.MustGet("allinfo").(config.AllInfo)
		if allinfo.Level&config.ADMIN1 == 0 {
			context.JSON(http.StatusOK, BaseResponse{
				Code:  10013,
				Error: ERROR10013,
			})
			context.Abort()
			return
		}
		var userInfo = config.UserInfo{}
		for i := range allinfo.Additionals {
			if allinfo.Additionals[i].Type == "admin1" {
				userInfo.Id = allinfo.Additionals[i].Id
				userInfo.Identity = "admin1"
				break
			}
		}
		context.Set("userinfo", userInfo)
		context.Next()
	}
}

// Admin2Middleware 是否是二级管理员
func Admin2Middleware() gin.HandlerFunc {
	return func(context *gin.Context) {
		allinfo := context.MustGet("allinfo").(config.AllInfo)
		if allinfo.Level&config.ADMIN2 == 0 {
			context.JSON(http.StatusOK, BaseResponse{
				Code:  10012,
				Error: ERROR10012,
			})
			context.Abort()
			return
		}
		var userInfo = config.UserInfo{}
		for i := range allinfo.Additionals {
			if allinfo.Additionals[i].Type == "admin2" {
				userInfo.Id = allinfo.Additionals[i].Id
				userInfo.Identity = "admin2"
				break
			}
		}
		context.Set("userinfo", userInfo)
		context.Next()
	}
}

// Admin3Middleware 是否是三级管理员
func Admin3Middleware() gin.HandlerFunc {
	return func(context *gin.Context) {
		allinfo := context.MustGet("allinfo").(config.AllInfo)
		if allinfo.Level&config.ADMIN3 == 0 {
			context.JSON(http.StatusOK, BaseResponse{
				Code:  10012,
				Error: ERROR10012,
			})
			context.Abort()
			return
		}
		var userInfo = config.UserInfo{}
		for i := range allinfo.Additionals {
			if allinfo.Additionals[i].Type == "admin3" {
				userInfo.Id = allinfo.Additionals[i].Id
				userInfo.Identity = "admin3"
				break
			}
		}
		context.Set("userinfo", userInfo)
		context.Next()
	}
}

var Admin = config.ADMIN1 | config.ADMIN2 | config.ADMIN3

// AdminMiddleware 判断是否是管理员
func AdminMiddleware() gin.HandlerFunc {

	return func(context *gin.Context) {
		allinfo := context.MustGet("allinfo").(config.AllInfo)
		if allinfo.Level&Admin == 0 {
			context.JSON(http.StatusOK, BaseResponse{
				Code:  10012,
				Error: ERROR10012,
			})
			context.Abort()
			return
		}
		var userInfo = config.UserInfo{}
		for i := range allinfo.Additionals {
			if strings.Contains(allinfo.Additionals[i].Type, "admin") {
				userInfo.Id = allinfo.Additionals[i].Id
				userInfo.Identity = allinfo.Additionals[i].Type
				break
			}
		}
		context.Set("userinfo", userInfo)
		context.Next()
	}
}

// ClerkMiddleware 是否是店员
func ClerkMiddleware() gin.HandlerFunc {
	return func(context *gin.Context) {
		allinfo := context.MustGet("allinfo").(config.AllInfo)
		// 判断是否有商家权限
		if allinfo.Level&config.CLERK == 0 {
			context.JSON(http.StatusOK, BaseResponse{
				Code:  10018,
				Error: ERROR10018,
			})
			context.Abort()
			return
		}
		var userInfo = config.UserInfo{}
		for i := range allinfo.Additionals {
			if allinfo.Additionals[i].Type == "clerk" {
				userInfo.Id = allinfo.Additionals[i].Id
				userInfo.Identity = "clerk"
			}
		}
		context.Set("userinfo", userInfo)
		context.Next()
	}
}
